package com.hshx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;;
import com.hshx.pojo.Record;
import com.hshx.pojo.Resume;
import com.hshx.service.RecordService;
import com.hshx.service.ResumeService;
import com.hshx.util.RedisUtil1;
import com.hshx.util.Util;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@CrossOrigin
@RequestMapping("/zhaopingService")
@RestController
public class ResumeController {

    private static String ALLOW_IMG_TYPE = "image/.png;image/.jpg;image/.gif;image/.jpeg";

    @Resource
    private ResumeService rmService;

    @Resource
    private RecordService rdService;

    @Autowired
    private RedisUtil1 redisUtil1;


    @RequestMapping(value = "/getOneResume")
    private Resume getOneResume(int resume_id){
        System.out.println("getOneResume");
        return rmService.getOne(new QueryWrapper<Resume>().eq("resume_id",resume_id));
    }

    @RequestMapping(value = "/addOneResume")
    public boolean addOneResume(Resume resume,String gdsj,int dazt) throws ParseException {
        System.out.println("addOneResume");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    //格式化规则
        Date dt2 = new Date(String.valueOf(sdf.parse(gdsj)));
        String strDate= sdf.format(dt2); //格式化成yyyy-MM-dd格式的时间字符串
        Date dt3 =sdf.parse(strDate);
        java.sql.Date resultDate = new java.sql.Date(dt3.getTime());


        //  档案编码由 dabm+8位随机数+hshx 组成
        String dabm = "dabm"+randowmMath()+"hshx";

//        //1、判断当前上传是否符合格式
//        String uploadFilePath = "D:\\y2Image\\picturebox\\";


        resume.setResume_state(1);
        boolean res = rmService.addResume(resume);
        boolean res2 = false;
        if (res){
            Record record = new Record();
            record.setRecord_no(dabm);
            record.setRecord_resume(rmService.getMaxResume());
            record.setRecord_date(resultDate);
            record.setRecord_delete(2);
            record.setRecord_using(dazt);
            res2 = rdService.addRecord(record);

        }
        return res2;
    }

    @RequestMapping(value = "/getAllResume")
    public List<Resume> getAllResume(){
        System.out.println("getAllResume");
        List<Resume> resumes = null;
        if (null != getRedis("resumeList",0,-1)){
            resumes = getRedis("resumeList",0,-1);
        }else{
            resumes = rmService.list(new QueryWrapper<Resume>()
                    .ne("resume_state",7));
            setRedis("resumeList",resumes);
        }
        return resumes;
    }

    @RequestMapping(value = "/getAllResumeSecond")
    public List<Resume> getAllResumeSecond(int pageIndex,int pageSize){
        System.out.println("List<Resume>()");

        List<Resume> resumes = null;
        /* 1。访问Redis，查看Redis是否保存当前集合的数据，如果存在就调用，不存在就加入Redis */
        if (null != getRedis("resumeListPage",pageIndex,pageSize)){
            resumes = getRedis("resumeListPage",pageIndex,pageSize);
        }else{
            PageHelper.startPage(pageIndex,pageSize);
            resumes = rmService.list(new QueryWrapper<Resume>()
                    .ne("resume_state",7));
            PageInfo<Resume> pageInfo = new PageInfo<Resume>(resumes);

            //2、存储简历集合
            setRedis("resumeListPage",resumes);
        }

        return resumes;
    }

    @RequestMapping(value = "/updResume")
    public boolean updResume(Resume resume){
        System.out.println("\n"+"updResume()");
        boolean res = rmService.updateById(resume);

        //重置Redis数据
        removeRedis("resumeList");

        //赋值Redis
        getAllResume();
        return res;
    }

    @RequestMapping(value = "/updResumeSecond")
    public boolean updResumeSecond(Resume resume,String resume_date1,String resume_datetime1) throws ParseException {
        System.out.println("\n"+"updResumeSecond()");


        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        resume.setResume_date(simpleDateFormat.parse(resume_date1));
        resume.setResume_datetime(simpleDateFormat.parse(resume_datetime1));

//        建议招聘，Java基础较好，熟练掌握Mysql性能优化，JDK-JRE-JVM区别
        boolean res = rmService.updateById(resume);

        //重置Redis数据
        removeRedis("resume_datetime");

        //赋值Redis
        getAllResumeSecond(0,-1);
        return res;
    }

    @RequestMapping(value ="/delResumeById")
    public boolean delResumeById(int resume_id){

        boolean res = rmService.removeById(resume_id);
        if (res){
            if (null != getRedis("resumeListPage",0,-1)){
                List<Resume> resumes = getRedis("resumeListPage",0,-1);

                Resume resume = new Resume();
                for (Resume rsm:resumes) {
                    if (rsm.getResume_id()==resume_id){
                        resume = rsm;
                        break;
                    }
                }
                resumes.remove(resume);
                removeRedis("resumeListPage");
                setRedis("resumeListPage",resumes);
            }
        }

        return res;
    }




    /*
    * 面试处理
    * */

    @RequestMapping(value = "/selInterviewer")
    public List<Resume> selInterviewer(int indexPage,int pageSize){
        List<Resume> resumes = null;
        //1、判断redis是否存储
        if (null != getRedis("interviewerListPage",indexPage,pageSize)){
            resumes = getRedis("interviewerListPage",indexPage,pageSize);
        }else{

            PageHelper.startPage(indexPage,pageSize);
            resumes = rmService.list(new QueryWrapper<Resume>()
                    .ne("resume_state",7));

            for (Resume res:resumes) {
                if (null != res.getResume_time()){
                    try {
                        java.sql.Date res1Date = Util.timeConvert(res.getResume_time());
                        res.setResume_time(res1Date);
                    } catch (ParseException e) {
                        throw new RuntimeException(e);
                    }
                }
            }

            setRedis("interviewerListPage",resumes);
        }

        return resumes;
    }

    @RequestMapping(value = "/selOneInterviewerById")
    public Resume selOneInterviewerById(int rid){
        Resume resume = rmService.getById(rid);
        try {
            if (null != resume.getResume_time()){
                resume.setResume_time(Util.timeConvert(resume.getResume_time()));
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        return resume;
    }

    @RequestMapping(value = "/updInterviewer")
    public boolean updInterviewer(Resume resume,String riqi) throws ParseException {
        System.out.println("updInterviewer()");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        resume.setResume_time((simpleDateFormat.parse(riqi)));
        boolean res = rmService.updateById(resume);

        //重置Redis数据
        removeRedis("interviewerListPage");
        //赋值Redis
        selInterviewer(0,2);
        return res;
    }

    @RequestMapping(value = "/delInterviewer")
    public boolean delInterviewer(int rid){
        Resume resume = new Resume();
        resume.setResume_interview_human("");
        resume.setResume_evaluate("");
        resume.setResume_id(rid);
        boolean res = rmService.updateById(resume);

        removeRedis("interviewerListPage");
        //赋值Redis
        selInterviewer(0,2);
        return res;
    }


    /*
    * 1、产生随机数的方法
    * */
    public String randowmMath(){
        String uuidRandom =
                String.valueOf(UUID.randomUUID().toString().replace("-","")
                        .substring(0,8));
        return uuidRandom;
    }

    /*
    * 2、Redis-增删查方法
    * */

    public void removeRedis(String name){
        System.out.println("removeRedis()");
        redisUtil1.remove(name);
    }

    public void setRedis(String name,List<Resume> resumes){
        System.out.println("setRedis()");
        redisUtil1.lPushAll(name,resumes,60L*24);
    }

    public List<Resume> getRedis(String name,int indexPage,int pageSize){
        System.out.println("getRedis()");
        List<Resume> resumes = null;
        List<Object> objects=redisUtil1.lRange(name, indexPage, pageSize);
        if(objects!=null&&objects.size()>0){
            resumes = objects.stream().map(j->(Resume)j).collect(Collectors.toList());

            /*
            * 每次执行getRedis 重新为Redis赋值，进行时间的刷新，保证时间上和用户访问一致
            * */
            removeRedis(name);
            setRedis(name,resumes);
            return resumes;
        }

        return null;
    }


    /* 熔断方法 */
    public String hystrix(String name){
        System.out.println("fallback provider:\t"+name);
        return "fallback_Provider:"+name;
    }

}
